cmake_minimum_required(VERSION 3.1)

find_package(Qt5 REQUIRED Test)

set(testdir ${CMAKE_BINARY_DIR}/TestDir)
file(MAKE_DIRECTORY ${testdir})
add_definitions(-DTEST_DIR=${testdir})
remove_definitions(-DMAKE_LIBQGIT2_LIB)

set(existing_repository ${testdir}/existing_repository)
add_definitions(-DTEST_EXISTING_REPOSITORY=${existing_repository})
if(NOT EXISTS ${existing_repository})
    message(STATUS "Creating test repository ...")
    file(MAKE_DIRECTORY ${existing_repository})
    file(MAKE_DIRECTORY ${existing_repository}/.git)
    file(GLOB_RECURSE gitfiles RELATIVE ${CMAKE_SOURCE_DIR}/.git ${CMAKE_SOURCE_DIR}/.git/*)
    foreach(gitfile ${gitfiles})
        configure_file(${CMAKE_SOURCE_DIR}/.git/${gitfile} ${existing_repository}/.git/${gitfile} COPYONLY)
    endforeach()
endif()
message(STATUS "Using test repository at '${existing_repository}'")

include_directories(${CMAKE_CURRENT_BINARY_DIR})

set(EXECUTABLE_OUTPUT_PATH  ${CMAKE_BINARY_DIR}/bin)

set(helper ${CMAKE_CURRENT_SOURCE_DIR}/TestHelpers)

macro(addTest name)
    set(_source ${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp)
    set(testname test${name})
    add_executable(${testname} ${_source} ${helper}.h ${helper}.cpp)
    target_link_libraries(${testname} qgit2 git2 Qt5::Core Qt5::Test)
    set_target_properties(${testname} PROPERTIES AUTOMOC ON)
    add_test(NAME ${testname} COMMAND ${testname})
endmacro()


addTest(Init)
addTest(Revision)
addTest(Clone)
addTest(Fetch)
addTest(Checkout)
addTest(Push)
addTest(Repository)
addTest(Diff)
addTest(Rebase)
